package com.esunny.resourcevisualization.config;

import com.esunny.resourcevisualization.controller.interceptor.LoginTicketInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author ：李先生
 * @date ：Created in 2020/7/19 16:14
 * @description： Web相关配置文件
 * @modified By：
 * @version: $
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private LoginTicketInterceptor loginTicketInterceptor;

    /**
     * 添加拦截器
     *
     * @param registry 拦截器注册库
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginTicketInterceptor)
                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg","/**/*.gif",
                        "/**/hello","/**/login","/**/excel/downloadTemplate/*","/**/excel/downloadResourceData/*","/**/process/viewProcessImage/*");
    }

    /**
     * 前后端分离跨域解决
     *
     * @param registry coors注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTIONS", "PUT")
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Authorization", "ticket")
                .allowCredentials(true).maxAge(36000);
//        "Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
//                        "Access-Control-Request-Headers"
    }
}
